import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class PaperSelection {
    private final Map<Paper, Student> selections = new HashMap<>();

    public boolean addSelection(Student student, Paper paper) {
        if (student == null || paper == null) {
            throw new NullPointerException("学生或论文不能为null");
        }
        if (selections.containsKey(paper)) {
            return false; // 论文已被选择
        }
        selections.put(paper, student);
        return true;
    }

    public boolean isSelected(Paper paper) {
        return selections.containsKey(paper);
    }

    public Map<Paper, Student> getSelections() {
        return new HashMap<>(selections); // 返回防御性拷贝，避免表示泄露
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<Paper, Student> entry : selections.entrySet()) {
            sb.append(entry.getKey().toString())
                    .append("\t selected by ")
                    .append(entry.getValue().getName())
                    .append("\n");
        }
        return sb.toString();
    }

    // 支持Visitor模式
    public void accept(PaperSelectionVisitor visitor) {
        visitor.visit(this);
    }
}